package com.shzq.dao.sqlUtil;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;
import java.util.Objects;

@Slf4j(topic = "provider")
public class CountMapperProvider {

    public String countByTimeQuantumWithDepth(Map map){
        return new SQL(){
            {
                SELECT("count(*)");
                FROM("earthquake");
                if(map.get("end")!=null) WHERE("year(occurrenceTime)=#{year} AND depth>=#{begin} AND depth<#{end}");
                else if(map.get("end")==null)WHERE("year(occurrenceTime)=#{year} AND depth>=#{begin}");
            }
        }.toString();
    }
    public String countByQuarterWithDepth(Map map){
        return new SQL(){
            {
                SELECT("count(*)");
                FROM("earthquake");
                if ("12".equals(map.get("month_begin")) && "2".equals(map.get("month_end"))) {
                    WHERE("month(occurrenceTime)=#{month_begin} OR month(occurrenceTime)<=#{month_end}");
                } else {
                    WHERE("month(occurrenceTime)>=#{month_begin} AND month(occurrenceTime)<#{month_end}");
                }
                if (map.get("end") == null) {
                    AND();
                    WHERE("depth>=#{begin}");
                }else {
                    AND();
                    WHERE("depth>=#{begin} AND strength<#{end}");
                }
//                log.info("sql为:{}",this.toString());
            }
        }.toString();
    }
    public String countByQuarterWithStrength(Map map){
        return new SQL(){
            {
                SELECT("count(*)");
                FROM("earthquake");
                if ("12".equals(map.get("month_begin")) && "2".equals(map.get("month_end"))) {
                    WHERE("month(occurrenceTime)=#{month_begin} OR month(occurrenceTime)<=#{month_end}");
                } else {
                    WHERE("month(occurrenceTime)>=#{month_begin} AND month(occurrenceTime)<#{month_end}");
                }
                if (map.get("end") == null) {
                    AND();
                    WHERE("strength>=#{begin}");
                } else {
                    AND();
                    WHERE("strength>=#{begin} AND strength<#{end}");
                }
//                log.info("sql为:{}",this.toString());
            }
        }.toString();
    }
    public String countByTimeQuantumWithStrength(Map map){
        return new SQL(){
            {
                SELECT("count(*)");
                FROM("earthquake");
                if(map.get("end")!=null) WHERE("year(occurrenceTime)=#{year} AND strength>#{begin} AND  strength<#{end}");
                else if(map.get("end")==null)WHERE("year(occurrenceTime)=#{year} AND  strength>#{begin}");
            }
        }.toString();
    }
}
